package com.binfenhulian.chinaenergy.ces.manage.controller.content;

import com.binfenhulian.chinaenergy.ces.core.bean.enums.ExceptionDescEnum;
import com.binfenhulian.chinaenergy.ces.core.exception.BaseException;
import com.binfenhulian.chinaenergy.ces.manage.bean.param.SessionParam;
import com.binfenhulian.chinaenergy.ces.core.bean.result.AjaxResult;
import com.binfenhulian.chinaenergy.ces.manage.controller.base.BaseController;
import com.binfenhulian.chinaenergy.ces.manage.framework.interceptor.ManageLoginCheck;
import com.binfenhulian.chinaenergy.ces.manage.service.account.OnlineService;
import com.binfenhulian.chinaenergy.ces.manage.service.content.ContentService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 内容管理
 * Created by wanghjbuf on 2018/3/15.
 */
@ManageLoginCheck
@Slf4j
@Controller
@RequestMapping("/manage/content")
public class ContentController extends BaseController {

    @Resource(name = "com.binfenhulian.chinaenergy.ces.manage.service.content.ContentServiceImpl")
    ContentService contentService;
    @Resource(name = "com.binfenhulian.chinaenergy.ces.manage.service.account.OnlineServiceImpl")
    OnlineService onlineService;

    @RequestMapping("/list.html")
    public String listContent(HttpServletRequest request, HttpServletResponse response) {
        return "manage/content";
    }

    @RequestMapping("/seedlist")
    @ResponseBody
    public Object seedList(HttpServletRequest request, HttpServletResponse response) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            Map<String,Object> paramMap = getParamMap(request);
            PageInfo page = contentService.listContentByPage(paramMap);
            ajaxResult = new AjaxResult(0, "查找成功", true, page);
        } catch (BaseException e){
            log.error(e.getMessage(), e);
            ajaxResult = new AjaxResult(0, e.getMessage(), false, null);
        } catch (Exception e) {
            log.error(ExceptionDescEnum.BASE.getExceptionDesc(), e);
            ajaxResult = new AjaxResult(0, ExceptionDescEnum.BASE.getExceptionDesc(), false, null);
        }
        return ajaxResult;
    }

    @RequestMapping("/publish")
    @ResponseBody
    public Object publish(HttpServletRequest request, HttpServletResponse response, String recordNo) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            SessionParam sessionParam = onlineService.getOnlineUserByRequest(request);
            contentService.publish(recordNo, sessionParam);
            ajaxResult = new AjaxResult(0, "查找成功", true, 0);
        } catch (BaseException e){
            log.error(e.getMessage(), e);
            ajaxResult = new AjaxResult(0, e.getMessage(), false, null);
        } catch (Exception e) {
            log.error(ExceptionDescEnum.BASE.getExceptionDesc(), e);
            ajaxResult = new AjaxResult(0, ExceptionDescEnum.BASE.getExceptionDesc(), false, null);
        }
        return ajaxResult;
    }

    @RequestMapping("/addSeed")
    @ResponseBody
    public Object addseed(HttpServletRequest request, HttpServletResponse response, String title, String url) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            if ("".equals(title)){
                throw new BaseException("标题不能为空,操作失败");
            }

            if ("".equals(url)) {
                throw new BaseException("起始url不能为空,操作失败");
            }

            SessionParam sessionParam = onlineService.getOnlineUserByRequest(request);
            int count = contentService.addSeed(title, url,sessionParam);
            ajaxResult = new AjaxResult(0, "查找成功", true, count);
        } catch (BaseException e){
            log.error(e.getMessage(), e);
            ajaxResult = new AjaxResult(0, e.getMessage(), false, null);
        } catch (Exception e) {
            log.error(ExceptionDescEnum.BASE.getExceptionDesc(), e);
            ajaxResult = new AjaxResult(0, ExceptionDescEnum.BASE.getExceptionDesc(), false, null);
        }
        return ajaxResult;
    }
}